package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.happyscan138.makefun.BaseAdapter.saokuGif;
import com.happyscan138.makefun.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;
import java.util.List;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/2/14.
 */

public class SaokuGifAdapter extends BaseAdapter {
    private Context mContext;
    private saokuGif bean;


    public void setSaogifKuAddClickListener(SaoKugifAddClickListener saogifKuAddClickListener) {
        this.saogifKuAddClickListener = saogifKuAddClickListener;
    }

    private SaoKugifAddClickListener saogifKuAddClickListener;


    public SaokuGifAdapter(Context mContext, List<saokuGif> listData) {
        this.mContext = mContext;
        this.llistData = listData;
    }

    private List<saokuGif> llistData;

    @Override
    public int getCount() {
        return llistData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder_saoku_gif viewHolder;
        //判断converView是否为空
        if(view==null){
            viewHolder=new ViewHolder_saoku_gif();
            view=View.inflate(mContext, R.layout.gif_item,null);
            viewHolder.saoku_gif=(GifImageView) view.findViewById(R.id.info_gif1_A);
            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_saoku_gif) view.getTag();
        }
         bean=llistData.get(position);
        //通过viewHolder找到对应控件，避免每次通过findViewById找控件
        viewHolder.saoku_gif.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(saogifKuAddClickListener != null) {
                    saogifKuAddClickListener.onItemClick(bean.gif);
                    log.e("giffffff",llistData.get(position).gif);

                }
            }
        });
        File saveImgPath = this.getImageDir(mContext);
        File gifSavePath1 = new File(saveImgPath, "gif1");
        displayImage(bean.gif, gifSavePath1, viewHolder.saoku_gif);
        String gif=bean.gif;
        log.e("gifxxxx",gif);

        return view;
    }

    public void displayImage(String url, File saveFile, final GifImageView gifView){
        HttpUtils http = new HttpUtils();
        // 下载图片
        http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {
            public void onSuccess(ResponseInfo<File> responseInfo) {
                try {
                    Log.e("tag", "onSuccess========" + responseInfo.result.getAbsolutePath());
                    GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );
                    gifView.setImageDrawable(gifFrom);
                }
                catch(Exception e){
                    Log.e("tag", e.getMessage());
                }
            }

            @Override
            public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) {
                Log.e("tag", "onFailure========" + s);
            }});
    }
    public File getImageDir(Context context){
        File file = getFilesDir(context, "images");
        return file;
    }
    public File getFilesDir(Context context, String tag){
        if(isSdCardExist() == true){
            return context.getExternalFilesDir(tag);
        }
        else{
            return context.getFilesDir();
        }
    }
    public boolean isSdCardExist() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }


    public  interface SaoKugifAddClickListener {
        // true add; false cancel
        public void onItemClick(String positionText); //传递boolean类型数据给activity
    }


}
class ViewHolder_saoku_gif{
    public GifImageView saoku_gif;
}